package com.foot.travel.activity;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.foot.travel.R;
import com.foot.travel.event.NetRefreshEvent;
import com.koalac.ironman.common.module.ModuleActivity;
import com.koalac.ironman.net.MyRequest;
import com.koalac.ironman.net.ResponseCallback;
import com.koalac.ironman.net.pojo.request.About;
import com.koalac.ironman.net.pojo.response.AboutDetail;
import com.koalac.ironman.net.pojo.response.Response;
import com.koalac.ironman.viewhelper.ViewInject;
import com.koalac.ironman.widget.LifeqImageView;
import com.koalac.ironman.widget.LoadingDialog;
import com.koalac.ironman.widget.ImageLoadAgreement.ImageType;
import com.squareup.otto.Subscribe;

public class AboutActivity extends ModuleActivity {

    @ViewInject(id=R.id.version)
    TextView mVersionView;
    @ViewInject(id=R.id.introduce)
    TextView mIntroduceView;
    @ViewInject(id=R.id.service_number)
    TextView mServiceNumberView;
    @ViewInject(id=R.id.parent_layout)
    View mParentLayout;
    @ViewInject(id=R.id.logo)
    LifeqImageView mLogoView;
    
    About mAbout;
    AboutDetail mAboutDetail;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setNavigationTitle("关于我们");
	setBackButtonVisibility(true);
	setContentLayoutBackgroudColor(getResources().getColor(R.color.module_color1));
	mParentLayout.setVisibility(View.GONE);
	init();
    }

    private void init() {
	mAbout = new About();
	final LoadingDialog  loadingDialog = showLoadingDialog("获取关于我们...");
	MyRequest.request(AboutActivity.this, mAbout, new ResponseCallback() {
	    
	    @Override
	    public void process(boolean result, Context context, Response data,
		    String msg) {
		canceledLoadingDialog(loadingDialog);
		if (result) {
		    mParentLayout.setVisibility(View.VISIBLE);
		    mAboutDetail = data.getObject(AboutDetail.class);
		    mVersionView.setText("当前版本:   v" + getVersion());
		    mIntroduceView.setText(mAboutDetail.getMessage());
		    mServiceNumberView.setText("服  务  号:  " + mAboutDetail.getServer_number());
		    mLogoView.loadCircleImage(mAboutDetail.getLogo(), ImageType.SourceImage);
		    setNetworkVisibility(false);
		}else {
		    setNetworkVisibility(true);
		}
	    }
	});
    }
    
    @Subscribe
    public void onRefresh(NetRefreshEvent netRefreshEvent) {
	setNetworkVisibility(false);
	init();
    }
    
    /**
	 * 获取版本号
	 * 
	 * @return 当前应用的版本号
	 */
	public String getVersion() {
		try {
			PackageManager manager = this.getPackageManager();
			PackageInfo info = manager.getPackageInfo(this.getPackageName(), 0);
			String version = info.versionName;
			return version;
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}
	}
    
    @Override
    protected void initLayout(ViewGroup rootContentView) {
	addContentView(R.layout.activity_about);
    }

}
